package com.bls.executor.node.sentence.simpleStmt;

import com.alibaba.fastjson.JSONObject;
import com.bls.action.BLSAction;
import com.bls.exception.MyException;
import com.bls.executor.parser.V3Parser;
import com.bls.executor.word.noun.Band;
import com.bls.executor.word.noun.Chatroom;
import com.bls.executor.word.noun.MessageContent;
import com.bls.executor.word.noun.User;
import com.bls.remote.service.IMessageToolRemoteService;
import com.bls.remote.service.Message;
import com.bls.utils.BandCore;


import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaoliang
 * @date 2021/12/28 18:48
 **/

public class NotifyStmt extends IntenseStmt {

    IMessageToolRemoteService messageToolRemoteService = null;

    {
        try {
            messageToolRemoteService = BLSAction.getBean(IMessageToolRemoteService.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private User user;
    private Band band;
    private Chatroom chatroom;
    private MessageContent messageContent;
    private HashMap<String, Object> param;


    public NotifyStmt(V3Parser.NotifyStmtContext ctx, Map<String, Object> scriptEnvironment) throws MyException {
        band = new Band(ctx.place().Identifier().getText(), scriptEnvironment);

        user = new User(ctx.Identifier().get(0).getText(), scriptEnvironment);
        chatroom = new Chatroom(ctx.Identifier().get(1).getText(), scriptEnvironment, band.getBandId());
        messageContent = new MessageContent(ctx.Identifier().get(2).getText());
    }


    @Override
    public boolean run(String methodName) {
        System.out.println("消息内容为：" + messageContent);
        Message message = new Message();
        message.setChatroomID(Long.valueOf(chatroom.getChatroomId()));
        message.setContent("【消息板测试消息】"+ messageContent.getText());
        try {
            JSONObject jsonObject = messageToolRemoteService.sendMessage(message);
            System.out.println(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        System.out.println(JSONObject.toJSONString(message));
        System.out.println("已经通知了，改剧本圆满结束！");
        return true;
    }

    public static void main(String[] args) {
        String httpurl = "https://www.wetoband.com/tre//actRunTool?gid=300000035&tp=e48e4a830ab9f14561c7a93754101336f2cef6317b17cb3b6323ef0190ebabcba74eb1bc1099854f2af21e5a6a362ce313262baf0abdd0337b929d72e3f2503a1fb71bfc7d5c1f7ea19bb4bbed293390020c0a0f6a8ef8f643f2cb8fc90234136c5e081e37e1a4f1ac94a4cd18658474fbdc418b42a612c1a974666727d09226bb0fde40d1af3b04530da08b572c6fbb6815c5d8544bbb2e58aa18937de39c66501dd33f5dd4f027635d609cbb5ffe23b8237ce483ec7c29851f04b27be7d6fa86f626765344ab6c67ee8c592a67c2a085bd437283b0d429a502cb1fb97276ecbca8df8f0aade2417f0dc7c27f8607b53d5cc1071b65ea94c6ac675f615950309c3fdabdb86950e958d58ed23ecb0d90ffff82da5950d0797b1ff6bb04fae73b9093f211e2317f19a687425c19ef858d35b8e3081d891007207b10c2f6139573bd763fb4aa31379c37a849db8a1255041a2ef2629efb73e99073d62d7ccfab08a247edd4bb9a9238e7fd6ae87df4ec0f81cb53352ab8ac33036936a8618628bad6a5826c60aba7d8f8d390f3933713d6bd4970a54799f5e07fdd403bd521b3821541092c1ff3504c858c77b3199e0b9403829b02096ebe48980c300788d5b1f8a728c6b230fd4c8c52c0b89d6ff1e29aeb9704b1736bf7d4d2926be015ab83d1c1f290e0b36f1ef572088bfa9c7df63cd6927dda05ec24a46e6b08c421c2bac41fcdd725cdfce434255ad73e8e4d5bc4e8bbb39ccf358d0a9793444d4cfb74f6d45a8432a00657d74f9301e3a5ab1c0b25cb9d0b2f109e22eea3372359a950a11b77cb495800c4578e1103973fcdd90849a574bc4745cc87c1a7268d2f75fffd55093055c7fdb8ee0a2f0793135f84eb05eecb0d4ed7667bdd50e8fbcd1f5436";
        String accessToken = "11d82abacb759279a7c278598c4b09d583b4540ba5907bcdab99575a9f775c7789cae916a83fe1e969b211d8a789a193757c9f47cae10bdf100d0a93a754671a";
        Map<String, String> params = new HashMap<>();

        params.put("action","sendMessages");
        params.put("message","{\"chatroomID\":\"314543796\",\"content\":\"测试消息\\r\\n\"}");
//        params.put("message","{\"receiverKeys\":[\"yoke\"],\"content\":\"测试消息\\r\\n\"}");
        String s = BandCore.doPostWithAccessToken(httpurl, accessToken, params);
        System.out.println("s = " + s);

    }
}
